<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/6/3
 * Time: 14:59
 */

namespace App\Setting\LazyCommon;

use App\Model\LazyCommon\AdminMenuModel;
use App\Model\LazyCommon\AdminMenuUserModel;
use App\Model\LazyCommon\UserGroupsModel;

class AdminMenuUserSetting{

    /*手动获取的配置项*/
    /*
     * 分类模型规则说明
     *      level
     *          最底层的目录parent（父级id） 为0
     *
     */
    public $catType = "level";//Options:level snake
    public $entityCnName = "菜单权限";
    public $table = "Admin_Menu_User";
    public $editKeys = array("menu","user");
    public $perPage = 100;

    /*自动获取的配置项*/
    public $entity;
    public $bundleName;
    public $entityName;
    public $SettingPath;
    public $entityPath;
    public $modelPath;
    public $doctrine;

    public function __construct($doctrine)
    {
        $this->bundleName = "App";
        $this->entityName = str_replace("\\","",str_replace("Setting","",str_replace(__NAMESPACE__,"",__CLASS__)));
        $this->SettingPath = __CLASS__;
        $this->entityPath = str_replace($this->entityName."Entity",$this->entityName,str_replace("Setting","Entity",str_replace("\\LazyCommon","",__CLASS__)));
        $this->entity = new $this->entityPath();
        $this->modelPath = str_replace("Setting","Model",__CLASS__);
        $this->doctrine = $doctrine;
    }

    public function listConfig()
    {
        $default = array();
        $default["Admin_Menu_User AS AdminMenuUser"]["select"] = array("id","menu","user");
        $default["Admin_Menu_User AS AdminMenuUser"]["search"] = array("menu","user");
        $default["Admin_Menu_User AS AdminMenuUser"]["condition"] = array("menu","user");
        $default["Admin_Menu_User AS AdminMenuUser"]["orderBy"] = array();

        $join = array();
        $join["User_Groups AS UserGroups"]["on"] = "UserGroups.id = AdminMenuUser.user";
        $join["User_Groups AS UserGroups"]["select"] = array("name");
        $join["User_Groups AS UserGroups"]["search"] = array();
        $join["User_Groups AS UserGroups"]["condition"] = array();
        $join["User_Groups AS UserGroups"]["orderBy"] = array();

        $join["Admin_Menu AS AdminMenu"]["on"] = "AdminMenu.id = AdminMenuUser.menu";
        $join["Admin_Menu AS AdminMenu"]["select"] = array("name","link");
        $join["Admin_Menu AS AdminMenu"]["search"] = array();
        $join["Admin_Menu AS AdminMenu"]["condition"] = array();
        $join["Admin_Menu AS AdminMenu"]["orderBy"] = array();

        return array(
            "default"   => $default,
            "joins"     => $join,
        );
    }

    public function formConfig()
    {
        $UserGroupsModel = new UserGroupsModel($this->doctrine);
        $UserGroups = $UserGroupsModel->getAllUserGroupsTree();
        $parents = array();
        foreach ($UserGroups as $k => $v) {
            $parents[$v["id"]] = $v["name"];
        }
        $options["user"]["other"]["choices"] = array_flip($parents);
        $config["user"] = array(
            "label"     =>"用户组",
            "showType"  =>"select",
            "dataType"  =>"required",
            "option"    =>$options["user"]
        );

        $parents=[];
        $model = new AdminMenuModel($this->doctrine);
        $categories = $model->getAllAdminMenuTree();
//        $parents[0] = "根目录";
        foreach ($categories as $c) {
            $parents[$c["id"]] = $c["name"];
        }
        $options["menu"]["other"] = array(
            "choices"   => array_flip($parents),
        );
        $config["menu"] = array(
            "label"     =>"按钮",
            "showType"  =>"checkbox",
            "dataType"  =>"required",
            "option"    => $options["menu"]
        );
        return $config;
    }
}